package com.dxyxx.framework;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.type.JavaType;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dxyxx.R;
import com.dxyxx.dialog.AgainPlayer;
import com.dxyxx.dialog.WaittingCircle;
import com.dxyxx.pojo.ProductDetail;
import com.dxyxx.pojo.ProductDetailItem;
import com.http.HttpUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ta.utdid2.android.utils.StringUtils;
import com.weixinninegridlayout.Image;
import com.weixinninegridlayout.NineAdapter;

public class ProductDetailActivity extends BaseActivity {
	private ListView listView;
	private List<List<Image>> imagesList;
	private List<String> words;
	private Bundle bundle;
	private ProgressDialog progressDialog;//全局等待框

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_productdetail);
		bundle = this.getIntent().getExtras();
		initViews();
		listView = (ListView) findViewById(R.id.lv_main);

		listView.setAdapter(new NineAdapter(ProductDetailActivity.this, imagesList, words));// 基本就是 单张图片

	}

	private void initViews() {
		context = this;
		ImageView backButton = ((ImageView) findViewById(R.id.backImageButton));
		backButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();

			}
		});
		((ImageView) findViewById(R.id.nextImageButton)).setVisibility(View.INVISIBLE);
		TextView titleView = (TextView) findViewById(R.id.title);
		titleView.setText("商品详情");
		titleView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);

		imagesList = new ArrayList<List<Image>>();
		words = new ArrayList<String>();
		callNetwork();
	}

	private Context context;

	// 通过网络获取数据
	protected void callNetwork() {
		// 启动等待框
		progressDialog = WaittingCircle.showDialog(context, false);
		progressDialog.show();
		String urlString = "productDetail/get";
		RequestParams rp = new RequestParams();
		rp.add("productId", bundle.getString("productId"));
		HttpUtil.get(urlString, rp, new AsyncHttpResponseHandler() {

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				AgainPlayer.makeCall((Activity) context, "网络好像出错了");
				progressDialog.dismiss();
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				try {
					System.out.println(new String(arg2));
					ProductDetail pd = getObjectMapper().readValue(arg2, ProductDetail.class);
					// 得到details 
					JavaType javaType = getObjectMapper().getTypeFactory().constructParametricType(List.class, ProductDetailItem.class);
					if (!StringUtils.isEmpty(pd.getDetails())) {
						List<ProductDetailItem> items = getObjectMapper().readValue(pd.getDetails(), javaType);
						for (ProductDetailItem item : items) {
							words.add(item.getDetail());
							List<Image> image = new ArrayList<Image>();
							String[] str = item.getImage().split("_");
							image.add(new Image(baseImageURL + str[0] + "?p=0", Integer.parseInt(str[1]), Integer.parseInt(str[2])));
							imagesList.add(image);
						}
					}
				} catch (JsonParseException e) {
					e.printStackTrace();
				} catch (JsonMappingException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					progressDialog.dismiss();
				}
			}

		});

	}
}
